package com.xsm.activitygroup;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.xsm.activitygroup.activity2.GetNewData;
import com.xsm.adapter.PaiHangAdapter;
import com.xsm.internet.PostMessage;
import com.xsm.object.PaihangObject;

import android.R.integer;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;

public class PaiHang extends Activity{
	PullToRefreshListView listView;
	ArrayList<PaihangObject> arrayList=new ArrayList<PaihangObject>();
	PaiHangAdapter padaAdapter;
	
	int state=0;
	private boolean scrollFlag = false;// 标记是否滑动
	private int lastVisibleItemPosition;// 标记上次滑动位置
	int getState=0;
	int page=0;
protected void onCreate(Bundle savedInstanceState) {
	requestWindowFeature(Window.FEATURE_NO_TITLE);
	super.onCreate(savedInstanceState);
	setContentView(R.layout.paihang);
	listView=(PullToRefreshListView)findViewById(R.id.paihangList);
	listView.setEnabled(false);
	padaAdapter=new PaiHangAdapter(arrayList,this);
	listView.setOnRefreshListener(new OnRefreshListener<ListView>() {
		public void onRefresh(PullToRefreshBase<ListView> refreshView) {
			String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
					DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
			refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
	  	 if(getState==0)
	  	 new getPai().execute();
		}
	});
	listView.setAdapter(padaAdapter);
	 listView.setOnScrollListener(new OnScrollListener() {
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
					scrollFlag = true;
					} else {
					scrollFlag = false;
					}
			}
			
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				if (scrollFlag) {
					if (firstVisibleItem > lastVisibleItemPosition) {
						state=1;
						//上滑
					}
					if (firstVisibleItem < lastVisibleItemPosition) {
						state=0;
						//下滑
					}
					if (firstVisibleItem == lastVisibleItemPosition) {
					return;
					}
					lastVisibleItemPosition = firstVisibleItem;
					}
			}
		});
	 new getPai().execute();
}

public void Back(View view){
	finish();
}





public class getPai extends AsyncTask<Void, Void, String>{
	protected String doInBackground(Void... arg0) {
		getState=1;
		if(state==0)
		page=0;
		else {
		page=arrayList.size();	
		}
		List param=new ArrayList();
		param.add(new BasicNameValuePair("ASK","GetPaihang"));
		param.add(new BasicNameValuePair("page",page+""));
		PostMessage postMessage=new PostMessage(param);
		String result=postMessage.getResult();
		try {
			JSONArray array=new JSONArray(result);
			if(state==0){
			if(array.length()>0)
			arrayList.clear();		
			}
			for(int i=0;i<array.length();i++){
				PaihangObject object=new PaihangObject();
				JSONObject jObject=array.optJSONObject(i);
				object.fenshu=jObject.getString("fenshu");
				object.issuecount=jObject.getString("issuecount");
				object.outcount=jObject.getString("outcount");
				object.user=jObject.getString("username");
			    arrayList.add(object);	
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	protected void onPostExecute(String result) {
		if(arrayList.size()<8)
			listView.setMode(Mode.PULL_DOWN_TO_REFRESH);
			else {
			listView.setMode(Mode.BOTH);
			}
			padaAdapter.notifyDataSetChanged();
			getState=0;
			listView.onRefreshComplete();
		super.onPostExecute(result);
	}
}
}
